from typing import List

from sqlalchemy import Enum, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from ._basic_ import BasicModel


class AIRoleType(Enum):
    """
    角色类型
    """
    HUMAN = 'human'
    """
    人类
    """
    PROGRAM = 'program'
    """
    程序
    """


class AIRoleGender(Enum):
    """
    角色性别
    """
    MALE = 'male'
    FEMALE = 'female'
    OTHER = 'other'


class AIRolePersonality(Enum):
    """
    角色性格
    """
    pass


# noinspection SpellCheckingInspection
class AIRole(BasicModel):
    """
    AI 角色
    """
    __tablename__ = 'ai_roles'
    __table_args__ = {'comment': 'AI 角色表'}

    name: Mapped[str] = mapped_column(String, unique=True, index=True, nullable=False, comment='角色名称')
    """
    角色名称
    """
    type: Mapped[AIRoleType] = mapped_column(AIRoleType, nullable=False, comment='角色类型')
    """
    角色类型
    """
    gender: Mapped[AIRoleGender] = mapped_column(AIRoleGender, nullable=True, comment='角色性别')
    """
    角色性别
    """
    age: Mapped[int] = mapped_column(Integer, nullable=True, comment='角色性别')
    """
    角色年龄
    """
    birthday: Mapped[str] = mapped_column(String(8), nullable=True, comment='角色生日')
    """
    角色生日
    采用 `YYYYMMDD` 格式表示
    """
    personality: Mapped[AIRolePersonality] = mapped_column(AIRolePersonality, nullable=True, comment='角色性格')
    """
    角色性别
    """
    additional_settings: Mapped[str] = mapped_column(Text, nullable=True, comment='补充设定')
    """
    补充设定
    """

    identities: 'Mapped[List[AIIdentity]]' = relationship('AIIdentity', back_populates='role')
    """
    关联的角色身份列表
    """


class AIIdentity(BasicModel):
    __tablename__ = 'ai_identities'
    __table_args__ = {'comment': 'AI 身份表'}

    role: Mapped[AIRole] = relationship('AIRole', back_populates='identities')
    """
    AI 角色
    """
    role_id: Mapped[int] = mapped_column(Integer, ForeignKey('ai_roles.id'), comment='AI 角色 ID')
    """
    AI 角色 ID
    """
    model = relationship('AIModel', back_populates='identities')
    """
    AI 模型
    """
    model_id: Mapped[int] = mapped_column(Integer, ForeignKey('ai_models.id'), comment='AI 模型 ID')
    """
    AI 模型 ID
    """
